package test

import java.util

import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.util.EntityUtils
import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}


object AntSooWang extends App {


//  val aa: List[String] = List("聚丙烯2009","聚丙烯2005","聚丙烯2001","聚丙烯2002").sorted.take(2)
//
//println(aa)
  //  login

//    val a = getFutureContractPriceList("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzczNTY3MTQsInVzZXJJZCI6IkJFRUJFIn0.orC7U0BnMPOisy52Qmo4RqaXdnYZ7hOZtHT7icXaaSI","LLDPE")
//    val b= getFutureContractPriceList("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzczNTY3MTQsInVzZXJJZCI6IkJFRUJFIn0.orC7U0BnMPOisy52Qmo4RqaXdnYZ7hOZtHT7icXaaSI","PP")
//    a.addAll(b)

  private val info: CrudeOilPriceInfo = getCrudeOilPricePageInfo("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1Nzc0MDkzOTgsInVzZXJJZCI6IkJFRUJFIn0.jP3Njms_ze6QbokX_NPV_tAFRFzYxZD_dqcRsoXlcPo", "布伦特")

  println(info)
//  getCrudeOilPricePageInfo("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzczNTY3MTQsInVzZXJJZCI6IkJFRUJFIn0.orC7U0BnMPOisy52Qmo4RqaXdnYZ7hOZtHT7icXaaSI", "WTI")
//  getCrudeOilPricePageInfo("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1NzczNTY3MTQsInVzZXJJZCI6IkJFRUJFIn0.orC7U0BnMPOisy52Qmo4RqaXdnYZ7hOZtHT7icXaaSI", "SC主力合约")


  // LLDPE PP
  def getFutureContractPriceList(token: String, categoryName: String) = {
    val httpClient = HttpClients.createDefault()
    val httpPost = new HttpPost("https://cloudapi.antsoo.com/crudeOil/futureContractPriceList")
    httpPost.addHeader("token", token)
    httpPost.addHeader("Content-Type", "application/json")
    httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36")
    httpPost.setEntity(new StringEntity("{\"categoryName\":\"" + stringToUnicode(categoryName) + "\",\"sort\":1,\"sortTpye\":0}"))
    val response = httpClient.execute(httpPost)
    println(response.getStatusLine().getStatusCode() + "\n")

    val entity = response.getEntity()
    val responseContent = EntityUtils.toString(entity, "UTF-8")
    println(responseContent)

    val jsonObj: JSONObject = JSON.parse(responseContent).asInstanceOf[JSONObject]
    val array: JSONArray = jsonObj.getJSONObject("body").getJSONArray("futureContractPriceList")
    array.remove(0)

    val list = new util.ArrayList[FutureContractPrice]()

    array.forEach(x => {
      val subArray = x.asInstanceOf[JSONObject].getJSONArray("priceList")
      val name = subArray.get(0).asInstanceOf[JSONObject].get("price").asInstanceOf[String]
      val price = Integer.parseInt(subArray.get(1).asInstanceOf[JSONObject].getString("price"))
      val change = Integer.parseInt(subArray.get(2).asInstanceOf[JSONObject].getString("price"))
      val changeRate = BigDecimal.apply(subArray.get(3).asInstanceOf[JSONObject].getString("price").replace("%", ""))
      list.add(new FutureContractPrice(name, price, change, changeRate))
    })

    response.close()
    httpClient.close()

    // 排序，取前2
    list.subList(0, 3)

  }

  // 获取原油价格 布伦特 WTI SC主力合约
  def getCrudeOilPricePageInfo(token: String, crudeOilCategoryName: String) = {
    val httpClient = HttpClients.createDefault()
    val httpPost = new HttpPost("https://cloudapi.antsoo.com/crudeOil/historyCrudeOilPricePageInfo")
    httpPost.addHeader("token", token)
    httpPost.addHeader("Content-Type", "application/json")
    httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36")
    httpPost.setEntity(new StringEntity("{\"userId\":61161,\"crudeOilCategoryName\":\"" + stringToUnicode(crudeOilCategoryName) + "\",\"pIndex\":1,\"pSize\":2}"))
    val response = httpClient.execute(httpPost)
    println(response.getStatusLine().getStatusCode() + "\n")

    val entity = response.getEntity()
    val responseContent = EntityUtils.toString(entity, "UTF-8")
    println(responseContent)

    val jsonObj: JSONObject = JSON.parse(responseContent).asInstanceOf[JSONObject]
    val dataObj = jsonObj.getJSONObject("body").getJSONArray("data").get(0).asInstanceOf[JSONObject]

    val price = Double.unbox(dataObj.getString("price"))
    val changeRangePrice = dataObj.getString("changeRangePrice")
    val changeRange = dataObj.getString("changeRange")
    val publishDate = dataObj.getString("publishDate")

    new CrudeOilPriceInfo(crudeOilCategoryName, price, changeRangePrice, changeRange, publishDate)

  }


  def login: String = {

    val httpClient = HttpClients.createDefault()
    val httpPost = new HttpPost("https://cloudapi.antsoo.com/user/login")
    httpPost.addHeader("Content-Type", "application/json")
    httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36")
    httpPost.setEntity(new StringEntity("{\"username\":\"18923096949\",\"password\":\"e10adc3949ba59abbe56e057f20f883e\"}"))
    val response = httpClient.execute(httpPost)
    println(response.getStatusLine().getStatusCode() + "\n")

    val entity = response.getEntity()
    val responseContent = EntityUtils.toString(entity, "UTF-8")
    println(responseContent)

    val jsonObj: JSONObject = JSON.parse(responseContent).asInstanceOf[JSONObject]
    val str: String = jsonObj.getJSONObject("body").getString("token")
    println(str)

    response.close()
    httpClient.close()
    str
  }

  def stringToUnicode(str: String): String = {
    val sb = new StringBuffer()
    val c = str.toCharArray()
    c.foreach(x => {
      if (x >= 19968 && x <= 171941) sb.append("\\u" + Integer.toHexString(x)) else sb.append(x)
    })
    sb.toString()
  }

}
